package com.xwm93up.basic.reflex;

import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Description: desc
 * @Author: xwm93up
 * @Since: 2023/2/25 14:38
 */
public class TestReflex {

    public static Class reflectClass() {
        Class student = null;
        try {
            student = Class.forName("com.xwm93up.basic.reflex.Student");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return student;
    }

    /**
     * 获取对象的所有公共属性
     */
    @Test
    public void test1() {
        Class student = reflectClass();
        Field[] fields = student.getFields();
        for (Field field : fields) {
            System.out.println(field);
        }
    }

    /**
     * 获取对象的所有属性,但不包括继承的
     */
    @Test
    public void test2() {
        Class student = reflectClass();
        Field[] declaredFields = student.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
    }

    /**
     * 获取对象所有的公共方法
     */
    @Test
    public void test3() {
        Class student = reflectClass();
        Method[] methods = student.getMethods();
        for (Method method : methods) {
            System.out.println(method);
        }
    }

    /**
     * 获取对象所有的方法,但不包含继承的
     */
    @Test
    public void test4() {
        Class student = reflectClass();
        Method[] declaredMethods = student.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
    }

    /**
     * 获取对象所有的公共构造方法
     */
    @Test
    public void test5() {
        Class student = reflectClass();
        Constructor[] constructors = student.getConstructors();
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
    }

    /**
     * 获取对象所有的构造方法
     */
    @Test
    public void test6() {
        Class student = reflectClass();
        Constructor[] declaredConstructors = student.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
    }

    /**
     * 实例化默认构造方法，调用set赋值
     */
    @Test
    public void test7() throws IllegalAccessException, InstantiationException {
        Class aClass = reflectClass();
        Student student = (Student) aClass.newInstance();
        student.setAddress("河北石家庄");
        System.out.println(student);
    }

    /**
     * 获取全部构造函数, 使用构造函数赋值
     */
    @Test
    public void test8() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        Class aClass = reflectClass();
        Constructor<Student> constructor = aClass.getConstructor(String.class, int.class, String.class, String.class);
        Student student = constructor.newInstance("zhangSan", 4, "小班", "石家庄");
        System.out.println(student);
    }

    /**
     * 获取方法, 并执行方法
     */
    @Test
    public void test9() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        Class aClass = reflectClass();
        Constructor<Student> constructor = aClass.getConstructor(String.class, int.class, String.class, String.class);
        Student student = constructor.newInstance("zhangSan", 4, "小班", "石家庄");
        Method showInfo = aClass.getMethod("showInfo");
        Object invoke = showInfo.invoke(student);
        System.out.println(invoke);
    }
}
